import ast

from utils.handle_db import orcl
from utils.handle_log import log,log_tojson
from utils.handle_attribute import HandleAttr

class CheckDB:

    def __init__(self):
        pass

    def __set_attribute(self,key,val):
        setattr(HandleAttr, key, val)

    def __get_db_result(self, sql):
        result = orcl.get_db_all_data(sql)
        return result

    def __replace_data(self,sql,key_list):
        log.info(msg="==============开始sql参数替换==============")
        if isinstance(sql,str):
            sql = sql
        else:
            sql =str(sql)
        log.info(msg="替换之前的sql:\n{},类型为{}".format(log_tojson(sql), type(sql)))
        for key in key_list:
            sql = sql.replace(f"#{key}#", str(getattr(HandleAttr, key)))
        log.info(msg="替换之后的sql:\n{},类型为{}".format(log_tojson(sql), type(sql)))
        log.info(msg="==============参数替换结束==============")
        return sql

    def check_db(self,check_db,replace_check_sql,expected_check_result):
        try:
            if check_db:
                check_db = ast.literal_eval(check_db)
                sql = check_db["sql"]
                if check_db and replace_check_sql:
                    replace_check_sql = ast.literal_eval(replace_check_sql)
                    log.info(msg="sql参数替换规则类型转换后\n{},类型为：{}".format(log_tojson(replace_check_sql),type(replace_check_sql)))
                    replace_key_list = [key for key in replace_check_sql.keys()]
                    log.info(msg="sql中需要替换的参数key列表\n{}".format(replace_key_list))

                    sql = self.__replace_data(sql, replace_key_list)
                    check_result = self.__get_db_result(sql)
                    log.info(msg="从db中获取到的数据:{}".format(check_result))
                    val = check_result[0][0]
                    if expected_check_result:
                        expected_check_result = ast.literal_eval(expected_check_result)
                        expected_result = expected_check_result["expected_result"]
                        assert expected_result == str(val)
                    else:
                        log.info(msg="数据库查询结果校验规则为空，无需对查询结果校验")

                    for key in replace_key_list:
                        self.__set_attribute(key=replace_check_sql[key],val=str(val))
                        log.info(msg=f"将查询结果保存到类属性中：{replace_check_sql[key]}={str(val)}")

                else:
                    log.info(msg="参数替换规则为空，不需要替换参数")
                    sql = sql
                    check_result = self.__get_db_result(sql)
                    log.info(msg="从db中获取到的数据:{}".format(check_result))
                    actual_result = check_result[0][0]
                    if expected_check_result:
                        expected_check_result = ast.literal_eval(expected_check_result)
                        expected_result = expected_check_result["expected_result"]
                        assert expected_result == str(actual_result)
                    else:
                        log.info(msg="数据库查询结果校验规则为空，无需对查询结果校验")

            else:
                log.info(msg="check_db为空，无需处理")
                return None
        except Exception as e:
            log.exception(e)
            raise AssertionError

